package com.dream.base.web.service.impl;
import org.apache.commons.fileupload.ProgressListener;

import com.dream.app.cache.ProgressCache;

/***
 * 
 * @copyright evan
 * @author evan
 * @Revision
 * @date 2013-9-15
 */
public class UploadProgressListener implements ProgressListener {
	
	private String key;
	
	private ProgressCache progressCache;
	
	/**
	 * 
	 * @param session 保存用户当前正在上传的文件进度
	 * @param fileElementId 区分用户上传的是哪个文件
	 */
	public UploadProgressListener(String key,ProgressCache progressCache) { 
		
		this.key = key;
		this.progressCache = progressCache;
	}
	
	/*
	 * pBytesRead 已经读取文件大小  pContentLength 文件总大小 pItems用于多文件上传(暂时不用)
	 * 保存文件上传的百分比0开始上传  100 上传完毕
	 * @see org.apache.commons.fileupload.ProgressListener#update(long, long, int)
	 */
	@Override
	public void update(long pBytesRead, long pContentLength,int pItems) {
		Long p = 	(pBytesRead*100)/pContentLength;
		p = p>=100?100L:p;
		progressCache.setProgress(key,p);
	}
}
